
var http = require('http');
/*
 * GET home page.
 */

exports.index = function(request, response){
    // Get weather of Harbin via http://cdn.weather.hao.360.cn/
    var api_url = 'http://cdn.weather.hao.360.cn/sed_api_weather_info.php?app=soComMap&&_callback=A&code=101050101';
    var response_string = '';
    var response_json;
    var req = http.get(api_url, function(res) {
        res.setEncoding('utf8');

        res.on('data', function (chunk) {
            response_string += chunk;   
        });

        res.on('end', function() {
            response_string = response_string.trim();
            var len = response_string.length;
            response_string = response_string.trim().substring(2, len - 2);
            response_json = JSON.parse(response_string);
            console.log(response_json);
            response.render('index', { title: 'Harbin Air', weather: response_json });
        });
    });
    req.on('error', function(e) {
        console.log("Problem with request: " + e.message);   
    });
};
